package net.zretc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import net.zretc.entity.User;
import net.zretc.mapper.UserMapper;
import net.zretc.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.List;

/**
 * 用户业务逻辑实现类
 * 实现用户相关的所有业务操作
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public List<User> findAll() {
        return userMapper.selectList(null);
    }

    @Override
    public int insert(User user) {
        return userMapper.insert(user);
    }

    @Override
    public int update(User user) {
        return userMapper.updateById(user);
    }

    @Override
    public int delete(Integer userId) {
        return userMapper.deleteById(userId);
    }

    @Override
    public User login(String username, String password) {
        // 使用QueryWrapper进行查询
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", username);
        queryWrapper.eq("password", password);

        User user = userMapper.selectOne(queryWrapper);
        if (user != null) {
            System.out.println("登录成功，用户信息: " + user);
        } else {
            System.out.println("登录失败，用户名: " + username + ", 密码: " + password);
        }
        return user;
    }

    @Override
    public boolean authenticate(String username, String password) {
        // 根据用户名查询用户
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", username);
        User user = userMapper.selectOne(queryWrapper);

        // 检查用户是否存在且密码正确
        if (user != null && user.getPassword().equals(password)) {
            return true;
        }
        return false;
    }

    @Override
    public boolean register(User user) {
        // 检查用户名是否已存在
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", user.getUsername());
        User existingUser = userMapper.selectOne(queryWrapper);

        if (existingUser != null) {
            System.err.println("注册失败：用户名已存在，查询到的用户: " + existingUser);
            return false;
        }

        // 设置创建时间
        user.setCreateTime(new Date());
        user.setAdmin(false); // 用户注册默认不是管理员

        try {
            int result = userMapper.insert(user);
            System.out.println("插入结果: " + result);
            return result > 0;
        } catch (Exception e) {
            System.err.println("注册插入数据库出错: " + e.getMessage());
            return false;
        }
    }

    @Override
    public boolean adminRegister(User user) {
        // 检查用户名是否已存在
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", user.getUsername());
        User existingUser = userMapper.selectOne(queryWrapper);

        if (existingUser != null) {
            System.err.println("注册失败：用户名已存在，查询到的用户: " + existingUser);
            return false;
        }

        // 设置创建时间
        user.setCreateTime(new Date());
        user.setAdmin(true); // 管理员注册设置为管理员

        try {
            int result = userMapper.insert(user);
            System.out.println("插入结果: " + result);
            return result > 0;
        } catch (Exception e) {
            System.err.println("注册插入数据库出错: " + e.getMessage());
            return false;
        }
    }
}